package com.hnqcgczy.supplier.controller;

import com.hnqcgczy.supplier.service.MarketService;
import com.hnqcgczy.supplier.service.MarketServiceImpl;
import com.hnqcgczy.supplier.utils.BaseServlet;
import com.hnqcgczy.supplier.vo.Market;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "MarketServlet", value = "/MarketServlet")
public class MarketServlet extends BaseServlet {
    //需要Service
    private MarketService marketService = new MarketServiceImpl();

    public void addMarket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Market market = new Market();
        //从前台获取值进行set操作
        market.setCompanyId(Integer.valueOf(request.getParameter("company_id") != null ? request.getParameter("company_id") : "0"));
        market.setMainClient(request.getParameter("main_client"));
        market.setMainCompetitor(request.getParameter("main_competitor"));
        market.setSupplier(request.getParameter("supplier"));
        market.setPrize(request.getParameter("prize"));
        market.setFactory(request.getParameter("factory"));
        market.setFactoryType(request.getParameter("factory_type"));
        market.setChainListedCompany(request.getParameter("chain_listed_company"));
        market.setOutListedCompany(request.getParameter("out_listed_company"));
        market.setLastYearTurnover(Double.valueOf(request.getParameter("last_year_turnover") != null ? request.getParameter("last_year_turnover") : "0"));
        market.setLastYearProfit(Double.valueOf(request.getParameter("last_year_profit") != null ? request.getParameter("last_year_profit") : "0"));
        //调添加的方法
        boolean flag = marketService.add(market);

        //传值
        //跳转
        response.sendRedirect("MarketServlet");

    }

    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //获取前台的数据
        String main_client = request.getParameter("main_client") != null ? request.getParameter("main_client") : "";
        String factory_type = request.getParameter("factory_type") != null ? request.getParameter("factory_type") : "";
        Market query = new Market();
        query.setMainClient(main_client);
        query.setFactoryType(factory_type);
        int pageSize = 10;
        int pageNow = request.getParameter("pageNow") != null ? Integer.parseInt(request.getParameter("pageNow")) : 1;
        //知道总页数需要总条数的
        int totals = marketService.total(query);
        int pages = totals % pageSize == 0 ? totals / pageSize : totals / pageSize + 1;
        if (pageNow >= pages) {
            pageNow = pages;
        }
        if (pageNow <= 1) {
            pageNow = 1;
        }
        List<Market> markets = marketService.queryByLike(query, pageNow, pageSize);

        //传值
        request.setAttribute("markets", markets);
        request.setAttribute("pageNow", pageNow);
        request.setAttribute("totals", totals);
        request.setAttribute("pages", pages);
        //跳转
        request.getRequestDispatcher("MarketInformation.jsp").forward(request, response);
    }

    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Market market = new Market();
        //从前台获取值进行set操作
        market.setMarketId(request.getParameter("market_id") != null ? Integer.parseInt(request.getParameter("market_id")) : 0);
        market.setCompanyId(Integer.valueOf(request.getParameter("company_id") != null ? request.getParameter("company_id") : "0"));
        market.setMainClient(request.getParameter("main_client"));
        market.setMainCompetitor(request.getParameter("main_competitor"));
        market.setSupplier(request.getParameter("supplier"));
        market.setPrize(request.getParameter("prize"));
        market.setFactory(request.getParameter("factory"));
        market.setFactoryType(request.getParameter("factory_type"));
        market.setChainListedCompany(request.getParameter("chain_listed_company"));
        market.setOutListedCompany(request.getParameter("out_listed_company"));
        market.setLastYearTurnover(Double.valueOf(request.getParameter("last_year_turnover") != null ? request.getParameter("last_year_turnover") : "0"));
        market.setLastYearProfit(Double.valueOf(request.getParameter("last_year_profit") != null ? request.getParameter("last_year_profit") : "0"));
        //
        boolean flag = marketService.update(market);
        //
        //传值
        //跳转
        response.sendRedirect("MarketServlet");
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int market_id = request.getParameter("market_id") != null ? Integer.parseInt(request.getParameter("market_id")) : 0;
        boolean flag = marketService.delete(market_id);
        //传值
        //跳转
        response.sendRedirect("MarketServlet");

    }
}
